package handlers

import (
	"github.com/gin-gonic/gin"
	"github.com/goccy/go-json"
	"order-man/cache"
	"order-man/models"
	"strconv"
	"time"
)

func RepoAdd(c *gin.Context) {
	repoCode := c.PostForm("repo_code")
	repoName := c.PostForm("repo_name")
	if repoCode == "" || repoName == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Repo{
		RepoCode: repoCode,
		RepoName: repoName,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}
func RepoOrderAdd(c *gin.Context) {
	orderCode := c.PostForm("order_code")
	orderFrom := c.PostForm("order_from")
	goodNum, _ := strconv.Atoi(c.PostForm("good_num"))
	price, _ := strconv.Atoi(c.PostForm("price"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	if orderCode == "" || orderFrom == "" || goodNum == 0 || price == 0 || rid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.RepoOrder{
		OrderCode: orderCode,
		OrderFrom: orderFrom,
		GoodNum:   goodNum,
		Price:     price,
		Rid:       rid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

/*
SELECT
  repo_orders.id AS RepoOrderId,
  repos.repo_code AS RepoCode,
  repos.repo_name AS RepoName,
  repo_orders.order_code AS OrderCode,
  repo_orders.order_from AS OrderFrom,
  repo_orders.good_num AS GoodNum,
  repo_orders.price AS Price
FROM
  repo_orders
  LEFT JOIN repos ON repo_orders.rid = repos.id
*/

func RopeOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var total int64
	models.DB.Model(models.RepoOrder{}).Count(&total)
	var list []models.RepoOrderList
	redis_key := "repo/order/list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("repo_orders").
			Select("repo_orders.id AS RepoOrderId",
				"repos.repo_code AS RepoCode",
				"repos.repo_name AS RepoName",
				"repo_orders.order_code AS OrderCode",
				"repo_orders.order_from AS OrderFrom",
				"repo_orders.good_num AS GoodNum",
				"repo_orders.price AS Price").
			Joins("LEFT JOIN repos ON repo_orders.rid = repos.id").
			Offset(offset).Limit(size).Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "列表查询成功",
		"data":  list,
		"total": total,
	})
	return
}
func RopeOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var repoOrder models.RepoOrderList
	redis_key := "repo/order/detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("repo_orders").
			Select("repo_orders.id AS RepoOrderId",
				"repos.repo_code AS RepoCode",
				"repos.repo_name AS RepoName",
				"repo_orders.order_code AS OrderCode",
				"repo_orders.order_from AS OrderFrom",
				"repo_orders.good_num AS GoodNum",
				"repo_orders.price AS Price").
			Joins("LEFT JOIN repos ON repo_orders.rid = repos.id").
			Where("repo_orders.id=?", id).
			Find(&repoOrder)
		jsonData, _ := json.Marshal(repoOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &repoOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "详情查询成功",
		"data": repoOrder,
	})
	return

}
